package com.ruoyi.crm.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.crm.domain.ContactPerson;
import com.ruoyi.crm.service.IContactPersonService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 联系人Controller
 *
 * @author fjc
 * @date 2021-08-25
 */
@RestController
@RequestMapping("/person")
public class ContactPersonController extends BaseController
{
    @Autowired
    private IContactPersonService contactPersonService;

    /**
     * 查询联系人列表
     */
    @PreAuthorize(hasPermi = "contact:person:list")
    @GetMapping("/list")
    public TableDataInfo list(ContactPerson contactPerson)
    {
        startPage();
        List<ContactPerson> list = contactPersonService.selectContactPersonList(contactPerson);
        return getDataTable(list);
    }

    /**
     * 导出联系人列表
     */
    @PreAuthorize(hasPermi = "contact:person:export")
    @Log(title = "联系人", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ContactPerson contactPerson) throws IOException
    {
        List<ContactPerson> list = contactPersonService.selectContactPersonList(contactPerson);
        ExcelUtil<ContactPerson> util = new ExcelUtil<ContactPerson>(ContactPerson.class);
        util.exportExcel(response, list, "联系人数据");
    }

    /**
     * 获取联系人详细信息
     */
    @PreAuthorize(hasPermi = "contact:person:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(contactPersonService.selectContactPersonById(id));
    }

    /**
     * 新增联系人
     */
    @PreAuthorize(hasPermi = "contact:person:add")
    @Log(title = "联系人", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ContactPerson contactPerson)
    {
        return toAjax(contactPersonService.insertContactPerson(contactPerson));
    }

    /**
     * 修改联系人
     */
    @PreAuthorize(hasPermi = "contact:person:edit")
    @Log(title = "联系人", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ContactPerson contactPerson)
    {
        return toAjax(contactPersonService.updateContactPerson(contactPerson));
    }

    /**
     * 删除联系人
     */
    @PreAuthorize(hasPermi = "contact:person:remove")
    @Log(title = "联系人", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(contactPersonService.deleteContactPersonByIds(ids));
    }

    /**
     * 获取联系人详细信息
     */
    @PreAuthorize(hasPermi = "contact:person:query")
    @GetMapping(value = "/contact/{customerName}")
    public AjaxResult getInfoByComName(@PathVariable("customerName") String customerName)
    {
        AjaxResult success = AjaxResult.success(contactPersonService.getInfoByComName(customerName));
        return success;
    }

    /**
     * 获取联系人详细信息
     */
    @PreAuthorize(hasPermi = "contact:person:query")
    @GetMapping(value = "/contact/telname/{telName}")
    public AjaxResult getInfoByTelName(@PathVariable("telName") String telName)
    {
        AjaxResult success = AjaxResult.success(contactPersonService.getInfoByTelName(telName));
        return success;
    }

    /**
     * 获取公司首要联系人详细信息
     */
    @PreAuthorize(hasPermi = "contact:person:query")
    @GetMapping(value = "/contact/first/{customerName}")
    public AjaxResult getFirstContactInfoByCustomerName(@PathVariable("customerName") String customerName)
    {
        AjaxResult success = AjaxResult.success(contactPersonService.getFirstContactInfoByCustomerName(customerName));
        return success;
    }
}